#include "mywidget.h"

#include <QDragEnterEvent>
#include "uieditorwidget.h"
#include <QDebug>
#include <QHBoxLayout>
MyWidget::MyWidget(QWidget *parent):QWidget(parent)
{
    this->setAcceptDrops(true);
    connect(&EventCenter::Instance(),&EventCenter::UIDelete,this,&MyWidget::DeleteChild);
}

MyWidget::~MyWidget()
{
    for(int i = 0;i < labels.count();i++)
    {
        MyLabel* m = labels[i];
        labels[i] = nullptr;
        delete m;
    }
    labels.clear();

    for(int i = 0;i < btns.count();i++)
    {
        MyButton* m = btns[i];
        btns[i] = nullptr;
        delete m;
    }
    btns.clear();
}

void MyWidget::AddBtn(ButtonInfo *info, int x, int y)
{
    MyButton* b = new MyButton(this);
    int id = 0;
    if(labels.count() > 0)
        id = labels.last()->id;
    if(btns.count() > 0)
        id = qMax(id,btns.last()->id);
    b->id = id + 1;
    b->move(x,y);
    b->Refresh(info);
    b->show();
    btns.push_back(b);
    if(myLayout != NONE_LAYOUT && this->layout())
    {
        this->layout()->addWidget(b);
    }
}

void MyWidget::AddLabel(LabelInfo *info,int x, int y)
{
    MyLabel* m = new MyLabel(this);
    int id = 0;
    if(labels.count() > 0)
        id = labels.last()->id;
    if(btns.count() > 0)
        id = qMax(id,btns.last()->id);
    m->id = id + 1;
    m->move(x,y);
    m->Refresh(info);
    m->show();
    labels.push_back(m);
    if(myLayout != NONE_LAYOUT && this->layout())
    {
        this->layout()->addWidget(m);
    }
}

void MyWidget::SetButton(ButtonInfo *info)
{
    btn = info;
    label = nullptr;
}

void MyWidget::SetLabel(LabelInfo *info)
{
    label = info;
    btn = nullptr;
}

void MyWidget::DeleteSelected()
{
    for(int i = 0;i < labels.count();i++)
    {
        if(labels[i]->IsSelected())
        {
            MyLabel* l = labels[i];
            labels.remove(i);
            delete l;
            return;
        }
    }
    for(int i = 0;i < btns.count();i++)
    {
        if(btns[i]->IsSelected())
        {
            MyButton* b = btns[i];
            btns.remove(i);
            delete b;
            return;
        }
    }
}

void MyWidget::RefreshBg(QString path)
{
    path.replace("\\","/");
    bgPath = path;
    if(this->objectName().isEmpty())
        this->setObjectName("widget");
    if(path.isEmpty())
        this->setStyleSheet("QWidget#" + this->objectName() + "{}");
    else
        this->setStyleSheet("QWidget#" + this->objectName() + "{border-image:url(\"" + path + "\");}");
}

QVector<MyLabel *> MyWidget::GetLabels()
{
    return labels;
}

QVector<MyButton *> MyWidget::GetButtons()
{
    return btns;
}

void MyWidget::PushLabel(MyLabel *l)
{
    labels.push_back(l);
}

void MyWidget::PushButton(MyButton *b)
{
    btns.push_back(b);
}

void MyWidget::SetLayout(LAYOUT layout)
{
    qDebug()<<"设置layout"<<layout;
    myLayout = layout;

    QLayout* old = this->layout();
    this->setLayout(nullptr);
    if(old)
        delete old;
    if(layout == NONE_LAYOUT)
    {
        return;
    }
    else if(layout == VERTICAL)
    {
        this->setLayout(new QVBoxLayout());
    }else if(layout == HORIZONTAL)
    {
        this->setLayout(new QHBoxLayout());
    }

    //将控件全部添加到布局中
    for(int i = 0;i < labels.count();i++)
    {
        this->layout()->addWidget(labels[i]);
    }
    for(int i = 0;i < btns.count();i++)
    {
        this->layout()->addWidget(btns[i]);
    }
}

void MyWidget::DeleteChild(QString name)
{
    for(int i = 0;i < labels.count();i++)
    {
        if(labels[i]->info && labels[i]->info->name == name)
        {
            MyLabel* l = labels[i];
            labels.remove(i);
            delete l;
            return;
        }
    }
    for(int i = 0;i < btns.count();i++)
    {
        if(btns[i]->info && btns[i]->info->name == name)
        {
            MyButton* b = btns[i];
            btns.remove(i);
            delete b;
            return;
        }
    }
}

void MyWidget::dragEnterEvent(QDragEnterEvent *event)
{
    event->acceptProposedAction();
}

void MyWidget::dropEvent(QDropEvent *event)
{
    QListWidget* u = dynamic_cast<QListWidget*>(event->source());
    if(!u)
        return;
    else
        qDebug()<<u->currentItem()->text();

    emit EventCenter::Instance().GetDragKJInfo();
    if(btn)
        AddBtn(btn);
    else if(label)
        AddLabel(label);
}

void MyWidget::resizeEvent(QResizeEvent *event)
{
    if(this->layout())
        return;
    QWidget::resizeEvent(event);
    int m_oldWidth = event->oldSize().width();
    int m_oldHeight = event->oldSize().height();

    int Width = this->width();
    int  Height = this->height();

    if(m_oldWidth <= 0 || m_oldHeight <= 0 || Width <= 0 || Height <=0)
        return;

    float  scaleX = Width*1.0/m_oldWidth*1.0;
    float  scaleY = Height*1.0/m_oldHeight*1.0;
    QList<QPushButton*> buttonList = findChildren<QPushButton*>();
    for(auto it = buttonList.begin();it != buttonList.end();it++)
    {
        int oldX = it.i->t()->x();
        int oldY = it.i->t()->y();
        int oldW = it.i->t()->width();
        int oldH = it.i->t()->height();
        it.i->t()->move(oldX * scaleX, oldY * scaleY);
        //it.i->t()->resize(oldW * scaleX, oldH * scaleY);
    }
    QList<QLabel*>labelList = findChildren<QLabel*>();
    for(auto it = labelList.begin();it != labelList.end();it++)
    {
        int oldX = it.i->t()->x();
        int oldY = it.i->t()->y();
        int oldW = it.i->t()->width();
        int oldH = it.i->t()->height();
        it.i->t()->move(oldX*scaleX, oldY*scaleY);
        //it.i->t()->resize(oldW*scaleX, oldH*scaleY);
    }
}
